<template>
  <el-card>
    <div slot="header"><span>订单管理</span></div>
    <!-- 查询表单 -->
    <el-form inline :model="formData" size="mini">
      <el-form-item label="订单号">
        <el-input v-model="formData.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input v-model="formData.consignee" placeholder="收货人"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formData.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="formData.orderState">
          <el-option value="已受理" label="已受理"></el-option>
          <el-option value="派送中" label="派送中"></el-option>
          <el-option value="已完成" label="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间">
        <el-date-picker
          v-model="formData.date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表表格 -->

    <el-table :data="tableData" border :style="{ width: w + 'px' }">
      <el-table-column label="订单号" prop="orderNo"></el-table-column>
      <el-table-column label="下单时间">
        <template slot-scope="scope">
          {{ scope.row.orderTime | formatHms }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="收货人" prop="consignee"></el-table-column>
      <el-table-column label="送货地址" prop="deliverAddress"></el-table-column>
      <el-table-column label="送达时间">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime | formatHms }}
        </template>
      </el-table-column>
      <el-table-column label="用户备注" prop="remarks"></el-table-column>
      <el-table-column label="订单金额" prop="orderAmount"></el-table-column>
      <el-table-column label="订单状态" prop="orderState"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope" @click="search(scope.row)">
          <el-button type="text" size="small" @click="Totalorder(scope.row.id)"
            >查看</el-button
          >
          <el-button type="text" size="small" @click="orderEdit(scope.row.id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="mt20"
      :page-size="pageSize"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      background
      layout="total,prev,pager,next,jumper"
    ></el-pagination>
  </el-card>
</template>

<script>
import resizeTable from "@/mixins/resizeTable";
import { getOrderListReq } from "@/api/order";
export default {
  mixins: [resizeTable],
  data() {
    return {
      //查询表单数据
      searchForm: {},
      formData: {
        orderNo: "", //订单号
        consignee: "", //收货人
        phone: "", //手机号
        orderState: "", //订单状态
        date: [], //日期
      },
      //需要修改的数据

      //表格数据
      tableData: [],
      //分页数据
      currentPage: 1,
      pageSize: 5,
      total: 0,
    };
  },
  methods: {
    async getData() {
      //如果为空报错解决
      let date = JSON.stringify(this.searchForm.date || "");
      //页码必发
      let currentPage = this.currentPage;
      let pageSize = this.pageSize;
      //发送请求，页码
      let res = await getOrderListReq({
        currentPage,
        pageSize,
        ...this.searchForm,
        date,
      });
      //解构数据
      let { total, data } = res.data;
      //数据赋值
      this.total = total;
      this.tableData = data;
    },
    //改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    search() {
      this.currentPage = 1; //页面重置
      this.searchForm = { ...this.formData }; //给查询表单赋值
      this.getData(); //再调用一次数据
    },
    Totalorder(x) {
      this.$router.push({
        path: "/order/order-detail",
        query: { id: x },
      });
    },
    orderEdit(x) {
      this.$router.push({
        path: `/order/order-edit/${x}`,
      });
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped></style>
